<template> 
	<FormCreate v-model:api="fApi" :option="option" :rule="rule" @submit="onFormSubmit"/>
</template>

<script>
import formCreate from "@form-create/element-ui";

import axios from '@/config/maxios'
	import util from '@/components/mdp-ui/js/util';//全局公共库
 	import { listFormDef } from '@/api/mdp/form/formDef'; 
	import { mapState } from 'pinia'
import { useUserStore } from '@/store/modules/user'   
	export default { 
	    computed: {
		    ...mapState(useUserStore,[
		      'userInfo'
		    ]) 
		},
		watch:{
			$route(to){ 
				this.init()
			}
		},
		data() {
			return {  
				fApi:null,
				formDef:null,
				rule:[],
				option:[],
			}
		},
		methods: { 
			onFormSubmit(formData,fcApi){
				
				var data=this.formDef 
				var option=this.option
				if(!option.form.crudApisCfg){
						this.$message.error("未配置接口参数，不支持提交数据")
						return;
				}
				var crudApisCfg=JSON.parse(option.form.crudApisCfg)
				if(!crudApisCfg.add){
						this.$message.error("未配置新增接口参数，不支持保存数据")
						return;
				}
				
				if(crudApisCfg.add.apiType=='2'){
						
						if(!crudApisCfg.add.params||!crudApisCfg.add.params.tableName){
								this.$message.error("未配置新增接口参数，不支持保存数据")
								return;
						} 
						this.execApi(crudApisCfg.add,formData,fcApi)
				}else if(crudApisCfg.list.apiType=='3'){
						if(!crudApisCfg.list.params||!crudApisCfg.list.params.dataSetId){
								this.$message.error("未配置列表接口参数，不支持直接查看数据")
								return;
						}
						var dataSetId=crudApisCfg.list.params.dataSetId 
						this.$router.push({path:`/mdp/dm/data/set/list/${dataSetId}`})
				}else if(crudApisCfg.list.apiType=='1'){
						this.$message.error("基于普通api的接口参数，不支持直接查看数据")
				}
			},
			init(){
				var formId=this.$route.params.formId
				if(!formId){
					return;
				}
				listFormDef({id:formId}).then(res=>{
					let {tips,data,total}=res
					if(tips.isOk){
						if(data.length>0){
							var formDef=data[0]
							if(formDef.options){
								this.option=JSON.parse(formDef.options)
							}
							if(formDef.rules){
								this.rule=JSON.parse(formDef.rules)
							} 
							this.formDef=formDef  
						}else{
							this.$message.error("没有查询到表单")
						}
					}else{
						this.$message.error(tips.msg)
					}
				})
			},
			
			parseJson(jsonStr){
				try{
					var json=JSON.parse(jsonStr)
					return json;
				} catch (e){
					return eval("("+jsonStr+")")
				}
				
			},
			tplParse(tplStr,formData){
				if(!tplStr){
					return tplStr
				}else{
					var userInfoKey="userInfo."
					var formDataKey="formData."
					const result = tplStr.replace(/\${(.*?)}/g, (match, key) => {
						var indexOf=key.indexOf(userInfoKey)
						if( indexOf>=0){
							var columnName=key.substring(userInfoKey.length)
							var camelName=this.$mdp.toCamel(columnName) 
							return "'"+this.userInfo[camelName]+"'" 
						}else{
							indexOf=key.indexOf(formDataKey)
							if(indexOf>=0){
								var columnName=key.substring(formDataKey.length)
								var camelName=this.$mdp.toCamel(columnName) 
								return "'"+formData[camelName]+"'" 
							}else{
								return match
							}
							
						} 
					});
					return result;
				}
			},
			execApi(api,formData,fcApi){
				
				var func=api.method=='get'?axios.get:axios.post
				var paramsStr=api.paramsStr
				if(paramsStr){
					paramsStr=this.tplParse(paramsStr,formData)
				} 
				var params={}
				if(paramsStr){
					params=this.parseJson(paramsStr)
				} 
				Object.assign(params,api.params)
				var p=Object.assign({},formData,params)
				func(api.action,api.method=='get'?{params:p}:p).then(res=>{
					let {tips,data}=res
					if(tips){
						if(tips.isOk){
							this.$message({message:tips.msg,type:'success'})
						}else{
							this.$message({message:tips.msg,type:'error'})
						}
					}else{
						this.$message({message:"接口正常，返回结果："+JSON.stringify(res),type:'success'})
					}
				}).catch(e=>{  
						this.$message({message:"接口异常，返回结果："+e.message,type:'error'}) 
				})
			},
		},
		components: {   
			FormCreate:formCreate.$form()
		},
		mounted() {    
			this.init()
		}
	}

</script>

<style scoped>

</style>